/*
 *  This file is part of Fax .NET.
 *
 *  Fax .NET is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation.
 * 
 *  Fax .NET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Fax .NET.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Drawing;

namespace Tourreau.Gilles.FaxDotNet
{
    /// <summary>
    /// Represent a fax job which is on a fax server.
    /// </summary>
    /// <remarks>Fax job are deleted after an fax was transmitted succesfully.</remarks>
    public class FaxJob
    {
        /// <summary>Recipient name</summary>
        private string recipientName;
        /// <summary>Recipient number</summary>
        private string recipientNumber;

        /// <summary>Sender company</summary>
        private string senderCompany;
        /// <summary>Sender department</summary>
        private string senderDepartment;
        /// <summary>Sender name</summary>
        private string senderName;

        /// <summary>Billing code</summary>
        private string billingCode;

        /// <summary>Job type</summary>
        private FaxJobType type;
        /// <summary>Job status</summary>
        private FaxStatus status;
        /// <summary>Queue status</summary>
        private FaxQueueStatus queueStatus;

        ///<summary>User name</summary>
        private string userName;

        /// <summary>Size</summary>
        private int size;
        /// <summary>Page count</summary>
        private int pageCount;

        /// <summary>Fax id (only use this for internal assembly).</summary>
        private uint id;

        /// <summary>Fax server attaching with this job</summary>
        private FaxServer server;

        /// <summary>
        /// Create a fax job.
        /// </summary>
        /// <param name="server">Fax server attaching with the new job.</param>
        /// <param name="entry">Data that will fill members of FaxJob.</param>
        internal FaxJob(FaxServer server, NativeMethods.FAX_JOB_ENTRY entry)
        {
            this.server = server;

            this.recipientName = entry.RecipientName;
            this.recipientNumber = entry.RecipientNumber;

            this.senderCompany = entry.SenderCompany;
            this.senderDepartment = entry.SenderDept;
            this.senderName = entry.SenderName;

            this.userName = entry.UserName;
            this.billingCode = entry.BillingCode;

            this.size = Convert.ToInt32(entry.Size);
            this.pageCount = Convert.ToInt32(entry.PageCount);

            this.id = entry.JobId;
            this.type = (FaxJobType)Enum.ToObject(typeof(FaxJobType), entry.JobType);

            this.Initialize(entry);
        }

        /// <summary>
        /// Initialise members variables which can be change.
        /// </summary>
        /// <param name="entry"></param>
        /// <remarks>This method was created for update members which can change (For example the Status of a fax job).
        /// This method is called by the constructor and the Refresh method.</remarks>
        private void Initialize(NativeMethods.FAX_JOB_ENTRY entry)
        {
            this.status = (FaxStatus)Enum.ToObject(typeof(FaxStatus), entry.Status);
            this.queueStatus = (FaxQueueStatus)Enum.ToObject(typeof(FaxQueueStatus), entry.QueueStatus);
        }

        /// <summary>
        /// Get the recipient number.
        /// </summary>
        public string RecipientNumber
        {
            get { return this.recipientNumber; }
        }

        /// <summary>
        /// Get the recipient name.
        /// </summary>
        public string RecipientName
        {
            get { return this.recipientName; }
        }

        /// <summary>
        /// Get the sender company.
        /// </summary>
        public string SenderCompany
        {
            get { return this.senderCompany; }
        }

        /// <summary>
        /// Get the sender department.
        /// </summary>
        public string SenderDepartment
        {
            get { return this.senderDepartment; }
        }

        /// <summary>
        /// Get the sender name.
        /// </summary>
        public string SenderName
        {
            get { return this.senderName; }
        }

        /// <summary>
        /// Get the fax job type.
        /// </summary>
        public FaxJobType Type
        {
            get { return this.type; }
        }

        /// <summary>
        /// Get the size of fax job.
        /// </summary>
        public int Size
        {
            get { return this.size; }
        }

        /// <summary>
        /// Get the page count of fax job.
        /// </summary>
        public int PageCount
        {
            get { return this.pageCount; }
        }

        /// <summary>
        /// Get the current status of fax job.
        /// </summary>
        public FaxStatus Status
        {
            get { return this.status; }
        }

        /// <summary>
        /// Get the billing code of fax job.
        /// </summary>
        public string BillingCode
        {
            get { return this.billingCode; }
        }

        /// <summary>
        /// Get the name of user who submitted the fax job
        /// </summary>
        public string UserName
        {
            get { return this.userName; }
        }

        /*public Image Image
        {
            get
            {
                if (this.image == null)
                    this.image = this.server.GetImage(this.ID);

                return this.image;
            }
        }*/

        /// <summary>
        /// Get the server where the fax job is.
        /// </summary>
        public FaxServer Server
        {
            get
            {
                if (this.server.CheckIsDisposed() == true)
                    throw new FaxException(FaxResources.ExceptionFaxServerDisposed);

                return this.server;
            }
        }

        /// <summary>
        /// Get the id job.
        /// </summary>
        /// <remarks>This ID must be use only on internal assembly.</remarks>
        internal uint ID
        {
            get { return this.id; }
        }

        /// <summary>
        /// Refresh some members of fax job.
        /// </summary>
        /// <param name="entry">Windows API structure of fax job entry.</param>
        internal void Refresh(NativeMethods.FAX_JOB_ENTRY entry)
        {
            this.Initialize(entry);
        }
    }
}
